using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RideMe.Presentation
{
	public partial class EmbeddedBrowser : Form
	{
		Image _disabledBack;
		Image _disabledForward;
		
		public EmbeddedBrowser()
		{
			InitializeComponent();

			GenerateDisabledImages();

			HookBrowserEvents();

			Load += delegate { back.Enabled = forward.Enabled = false; };

            this.KeyUp += new KeyEventHandler(EmbeddedBrowser_KeyUp);
		}

		/// <summary>
		/// Prepare to respond to browser events and nav button clicks.
		/// </summary>
		private void HookBrowserEvents()
		{
			ie.CanGoBackChanged += delegate { back.Enabled = ie.CanGoBack; };
			ie.CanGoForwardChanged += delegate { forward.Enabled = ie.CanGoForward; };

			back.EnabledChanged += delegate { back.BackgroundImage = back.Enabled ? Properties.Resources.NavBack : _disabledBack; };
			forward.EnabledChanged += delegate { forward.BackgroundImage = forward.Enabled ? Properties.Resources.NavForward : _disabledForward; };

			back.Click += delegate { ie.GoBack(); };
			forward.Click += delegate { ie.GoForward(); };
			refresh.Click += delegate { ie.Refresh(); };
			address.GotFocus += delegate { address.SelectAll(); };

		}


		/// <summary>
		/// Generate disabled images of the nav buttons.
		/// </summary>
		private void GenerateDisabledImages()
		{
			_disabledBack = DrawImageDisabled(Properties.Resources.NavBack);
			_disabledForward = DrawImageDisabled(Properties.Resources.NavForward);
		}

		/// <summary>
		/// Generate a gray-scale "disabled" image based on the enabled version of the image.
		/// </summary>
		/// <param name="image">The enabled version of the image.</param>
		/// <returns>The new disabled image.</returns>
		private System.Drawing.Bitmap DrawImageDisabled(System.Drawing.Image image)
		{
			System.Drawing.Bitmap active = new Bitmap(image);
			System.Drawing.Bitmap disable = new Bitmap(active.Width, active.Height);
			System.Drawing.Graphics g = Graphics.FromImage(disable);

			g.DrawImage(active, 0, 0);
			ControlPaint.DrawImageDisabled(g, active, 0, 0, Color.Empty);
			g.Dispose();
			return disable;
		}

		/// <summary>
		/// Navigate the browser to a specific url.
		/// Refreshes the browser the url is already the current page.
		/// </summary>
		/// <param name="url">The url to browse to.</param>
		public void Navigate(string url)
		{
			if (ie.Url == null || ie.Url.ToString() != url)
				ie.Navigate(url);
			else
				ie.Refresh();
		}
		
		/// <summary>
		/// Navigate forward.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void forward_Click(object sender, EventArgs e)
		{
			ie.GoForward();
		}

		/// <summary>
		/// Update the address bar after navigation is successful.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ie_Navigated(object sender, WebBrowserNavigatedEventArgs e)
		{
			address.Text = e.Url != null ? ie.Url.ToString() : string.Empty;
			ie.Focus();
		}
		
		
		/// <summary>
		/// Translate the Enter key in the address bar as "Go".
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void address_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyValue == 13)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				Navigate(address.Text);
			}
		}

        // Unfortunately there are two different event signatures, so we can't share
        // an event here. Ugly! Duplication! Ahh! And I can't even make a common
        // method that both handlers call because the keyventargs are different.
        // Grrrr!!!!!!!

        private void ie_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.L))
            {
                address.Focus();
            }
        }

        private void EmbeddedBrowser_KeyUp(object sender, KeyEventArgs e)
        {
            if ((e.Control) && (e.KeyCode == Keys.L))
            {
                address.Focus();
            }
        }

	}
}